package com.wb3.meta.api.core.controller;


import com.wb3.meta.api.core.service.MyToysService;
import com.wb3.meta.api.core.vo.ReqMyToysVo;
import com.wb3.meta.common.annotation.RequireAuth;
import com.wb3.meta.common.authentication.RequestHolder;
import com.wb3.meta.common.pojo.ResultT;
import com.wb3.meta.common.utils.UploadFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@RestController
@Api(tags = "通用接口")
@RequestMapping("base/common")
public class CommonController {
    @Autowired
    private UploadFileUtils uploadFileUtils;
    @Autowired
    private MyToysService myToysService;
    @RequireAuth
    @ApiOperation("图片上传")
    @PostMapping("upload")
    public ResultT upload(@RequestBody MultipartFile file,String path) {
        // 获取文件后缀名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 定义允许的图片格式
        Set<String> allowedFormats = new HashSet<>(Arrays.asList(".jpg", ".jpeg", ".png", ".gif"));

        // 检查文件格式
        if (!allowedFormats.contains(suffix.toLowerCase())) {
            // 如果文件格式不正确，返回错误信息
            return ResultT.fail("只能上传图片");
        }

        // 生成新的文件名
        String filename = System.currentTimeMillis() + "-" + RequestHolder.getUserId() + suffix;

        // 上传文件
        return uploadFileUtils.uploadFileToAliyun(path, filename, file);
    }

    @ApiOperation("文字转tts")
    @PostMapping("text2tts")
    public ResultT textToTts(@RequestBody ReqMyToysVo reqMyToysVo) {
        return myToysService.textToTts(reqMyToysVo);
    }

}
